package ch.uzh.ifi.seal.ssqa.ui.views;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.part.ViewPart;

import ch.uzh.ifi.seal.ssqa.metrics.CyclomaticComplexityMetric;
import ch.uzh.ifi.seal.ssqa.metrics.IMetric;
import ch.uzh.ifi.seal.ssqa.metrics.LinesOfCodeMetric;
import ch.uzh.ifi.seal.ssqa.metrics.NumberOfClassesMetric;
import ch.uzh.ifi.seal.ssqa.metrics.NumberOfFilesMetric;
import ch.uzh.ifi.seal.ssqa.metrics.NumberOfModulesMetric;
import ch.uzh.ifi.seal.ssqa.model.DiagramComponent;

public class NumericMetricTableView extends ViewPart implements ISelectionListener {

	public static final String ID = "ch.uzh.ifi.seal.ssqa.ui.views.NumericMetricTableView";
	
	
	private TableViewer viewer;
	
	@Override
	public void selectionChanged(IWorkbenchPart part, ISelection selection) {
		for(int i = this.viewer.getTable().getItemCount(); i>0; i--){
			this.viewer.remove(this.viewer.getElementAt(i-1));
		}
		if(part instanceof ArchitectureAnalysisView) {
			List<GraphicalEditPart> editParts = new ArrayList<GraphicalEditPart>();
			List<DiagramComponent> elements = new ArrayList<DiagramComponent>();
			editParts = ((ArchitectureAnalysisView)part).getViewer().getSelectedEditParts();
			
			if(editParts.size()>0){
				for(GraphicalEditPart edit : editParts){
					View view = (View)edit.getModel();
					DiagramComponent element = (DiagramComponent)view.getElement();
					elements.add(element);
				}
				this.viewer.add((new LinesOfCodeMetric()).calculateResult(elements.get(0)));
				this.viewer.add((new NumberOfFilesMetric()).calculateResult(elements.get(0)));
				this.viewer.add((new CyclomaticComplexityMetric()).calculateResult(elements.get(0)));
				this.viewer.add((new NumberOfClassesMetric()).calculateResult(elements.get(0)));
				this.viewer.add((new NumberOfModulesMetric()).calculateResult(elements.get(0)));
			}
		}
	}

	@Override
	public void createPartControl(Composite parent) {
		this.viewer = new TableViewer(parent);
		final Table table = viewer.getTable();
		table.setHeaderVisible(true);
		table.setLinesVisible(true);

		this.defineColumns();
		
		this.getSite().getPage().addSelectionListener(this);
	}
	
	private TableViewerColumn createTableViewerColumn(String title, int bound, final int colNumber) {
		final TableViewerColumn viewerColumn = new TableViewerColumn(this.viewer, SWT.NONE);
		final TableColumn column = viewerColumn.getColumn();
		column.setText(title);
		column.setWidth(bound);
		column.setResizable(true);
		column.setMoveable(true);
		return viewerColumn;
	}


	@Override
	public void setFocus() {
		// TODO Auto-generated method stub
		
	}
	
	private void defineColumns(){
		TableViewerColumn col = createTableViewerColumn("Metric", 150, 0);
		col.setLabelProvider(new ColumnLabelProvider() {
			@Override
			public String getText(Object element) {
				return ((IMetric)element).getDescription();
			}
		});

		TableViewerColumn col2 = createTableViewerColumn("Value", 200, 0);
		col2.setLabelProvider(new ColumnLabelProvider() {
			@Override
			public String getText(Object element) {
				return Double.toString(((IMetric)element).getResult());
			}
		});
	}
}
